﻿using GUI.RLAServiceReference;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace GUI
{
    /// <summary>
    /// Interaction logic for UIUserAchievementPopUp.xaml
    /// </summary>
    public partial class UIUserAchievementPopUp : Window
    {
        private IRealLifeService _service;
        private UserAchievement _shownUserAchievement;

        public UIUserAchievementPopUp(UserAchievement userAchievement)
        {
            _service = new RealLifeServiceClient();
            _shownUserAchievement = userAchievement;

            InitializeComponent();

            ShowUserAchievement();

            CheckStatus();
        }

        private void ShowUserAchievement()
        {
            txtCategory.Text = _shownUserAchievement.Achievement.Category;
            txtDescription.Text = _shownUserAchievement.Achievement.Description;
            txtStatus.Text = _shownUserAchievement.CurrentStatus.ToString();
            txtTags.Text = _shownUserAchievement.Achievement.Tag;
            txtTitle.Text = _shownUserAchievement.Achievement.Title;
        }

        private void CheckStatus()
        {
            if (_shownUserAchievement.Achievement.CurrentStatus == Achievement.Status.InActive)
            {
                btnRight.Visibility = Visibility.Collapsed;
                btnCenter.Visibility = Visibility.Collapsed;
                btnLeft.Margin = new Thickness(179, 278, 0, 0);
                
                if(_shownUserAchievement.CurrentStatus == UserAchievement.Status.InProgress)
                    GiveUpAchievement();
                else if(_shownUserAchievement.CurrentStatus == UserAchievement.Status.Invited)
                    RejectInvitation();
            }
            
            else
            {
                if (_shownUserAchievement.Achievement.AchievementStarterId == _shownUserAchievement.User.UserId)
                {
                    btnUnder.Visibility = Visibility.Visible;
                }

                if (_shownUserAchievement.CurrentStatus == UserAchievement.Status.Invited)
                {
                    btnRight.Content = "Acceptér";
                    btnCenter.Content = "Afslå";
                }
                else if (_shownUserAchievement.CurrentStatus == UserAchievement.Status.InProgress)
                {
                    btnRight.Content = "Gennemfør";
                    btnCenter.Content = "Opgiv";
                }
                else if (_shownUserAchievement.CurrentStatus == UserAchievement.Status.Done ||
                         _shownUserAchievement.CurrentStatus == UserAchievement.Status.Failed ||
                         _shownUserAchievement.CurrentStatus == UserAchievement.Status.Rejected)
                {
                    btnRight.Visibility = Visibility.Collapsed;
                    btnCenter.Visibility = Visibility.Collapsed;
                    btnUnder.Margin = new Thickness(179, 278, 0, 0);
                }
            }
        }

        private void FinishAchievement()
        {
            _shownUserAchievement.CurrentStatus = UserAchievement.Status.Done;
            SetStatus();
        }

        private void AcceptInvitation()
        {
            _shownUserAchievement.CurrentStatus = UserAchievement.Status.InProgress;
            SetStatus();
        }

        private void RejectInvitation()
        {
            _shownUserAchievement.CurrentStatus = UserAchievement.Status.Rejected;
            SetStatus();
        }

        private void GiveUpAchievement()
        {
            _shownUserAchievement.CurrentStatus = UserAchievement.Status.Failed;
            SetStatus();
        }

        private void EndAchievement()
        {
            _shownUserAchievement.Achievement.CurrentStatus = Achievement.Status.InActive;

            int result = -1;

            try
            {
                result = _service.SetAchievementStatus(_shownUserAchievement.Achievement);
            }
            catch (Exception e)
            {
                MessageBox.Show("Something went wrong! Stack trace: " + e.StackTrace);
            }
        }

        private void SetStatus()
        {
            int result = -1;

            try
            {
                result = _service.SetUserAchievementStatus(_shownUserAchievement);
            }
            catch (Exception e)
            {
                MessageBox.Show("Something went wrong! Stack trace: " + e.StackTrace);
            }
        }

        private void btnRight_Click(object sender, RoutedEventArgs e)
        {
            if (_shownUserAchievement.CurrentStatus == UserAchievement.Status.Invited)
            {
                AcceptInvitation();
                this.Close();
            }
            else if (_shownUserAchievement.CurrentStatus == UserAchievement.Status.InProgress)
            {
                FinishAchievement();
                this.Close();
            }
        }

        private void btnCenter_Click(object sender, RoutedEventArgs e)
        {
            if (_shownUserAchievement.CurrentStatus == UserAchievement.Status.Invited)
            {
                RejectInvitation();
                this.Close();
            }
            else if (_shownUserAchievement.CurrentStatus == UserAchievement.Status.InProgress)
            {
                GiveUpAchievement();
                this.Close();
            }
        }

        private void btnLeft_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnUnder_Click(object sender, RoutedEventArgs e)
        {
            EndAchievement();
            if(_shownUserAchievement.CurrentStatus == UserAchievement.Status.InProgress)
                GiveUpAchievement();
            this.Close();
        }
    }
}